package com.huawei.hwmdemo;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.huawei.cloudlink.openapi.HWMSdk;
import com.huawei.cloudlink.openapi.OpenSDKConfig;


import com.huawei.hwmdemo.mqtt.IGetMessageCallBack;
import com.huawei.hwmdemo.mqtt.MQTTService;
import com.huawei.hwmdemo.mqtt.MyServiceConnection;
import com.huawei.hwmdemo.utils.PreferencesUtil;

public class MyApplication extends Application implements IGetMessageCallBack {

    private MyServiceConnection serviceConnection;
    private MQTTService mqttService;

    String account = "18751439680";
    String password = "jndbd0804";

    @Override
    public void onCreate() {
        super.onCreate();

        OpenSDKConfig sdkConfig = new OpenSDKConfig(this)
//                .setAppId(getAppId()) //向会议服务器申请的appid
                .setNeedConfChat(true)
                .setNeedScreenShare(true)
                .setNeedFeedback(false);

        //-----------------------------------------------------应用初始化--------------------------------------------------------
        HWMSdk.init(this, sdkConfig);
        Log.e("TAG", "初始化");

        // mqtt
        serviceConnection = new MyServiceConnection();
        serviceConnection.setIGetMessageCallBack(MyApplication.this);
        Intent intent = new Intent(getApplicationContext(), MQTTService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void setMessage(String message) {
        mqttService = serviceConnection.getMqttService();
        mqttService.toCreateNotification(message);
    }


//    private void login() {
//        LoginParam loginParam = new LoginParam()
////                .setLoginAuthType(LoginAuthType.Account_And_Password)       //登录方式的类别
//                .setAccount(account)        //登录帐号
//                .setPassword(password);      //登录密码
//
//        HWMSdk.getOpenApi(this).login(loginParam, new HwmCallback<LoginResult>() {
//            @Override
//            public void onSuccess(LoginResult loginResult) {
//                if (loginResult != null) {
//                    Log.e("TAG", loginResult.getUserUuid());
//                } else {
//                    Log.e("TAG", "登录成功");
//                }
//            }
//
//            @Override
//            public void onFailed(int retCode, String desc) {
//                Log.e("TAG", String.valueOf(retCode));
//                Log.e("TAG", "登录错误：" + DemoUtil.getErrMsg(getApplicationContext(), retCode));
//            }
//        });



//        String account = "18476925171";
//        String password = "2262093abc";
//
//        LoginParam loginParam = new LoginParam()
//                .setLoginAuthType(LoginAuthType.Account_And_Password)
//                .setAccount(account)
//                .setPassword(password);
//
//        HWMSdk.getOpenApi(this).login(loginParam, new HwmCallback<LoginResult>() {
//            @Override
//            public void onSuccess(LoginResult loginResult) {
//                if (loginResult != null) {
//                    DemoUtil.showToast("登录成功" + loginResult.getUserUuid());
//                } else {
//                    DemoUtil.showToast("已登录");
//                }
//            }
//
//            @Override
//            public void onFailed(int retCode, String desc) {
//                HwmContext.getInstance().runOnMainThread(() -> {
//                    DemoUtil.showToast("登录错误：" + DemoUtil.getErrMsg(getApplicationContext(), retCode));
//                    Log.e("TAG", String.valueOf(retCode));
//                    Log.e("TAG", "登录错误：" + DemoUtil.getErrMsg(getApplicationContext(), retCode));
//                });
//            }
//        });
//    }



//    private void joinConf() {
//
//        String mId = "938511663";
//        String nickName = "Android";
//        String password = "993350";
//        if (TextUtils.isEmpty(mId)) {
//            DemoUtil.showToast("会议ID不能为空");
//            return;
//        }
//
//
//        JoinConfParam joinConfParam;
//        joinConfParam = new PasswordJoinConfParam()
//                .setPassword(password)
//                .setConfId(mId)
//                .setNickname(nickName)
//                .setCameraOn(true)
//                .setMicOn(true);
//
//        HWMSdk.getOpenApi(this).joinConf(joinConfParam, new HwmCancelableCallBack<Void>() {
//            @Override
//            public void onSuccess(Void ret) {
//                Log.e(TAG, "成功");
//            }
//
//            @Override
//            public void onFailed(int retCode, String desc) {
//                String err = ErrorMessageFactory.create(Utils.getApp(), retCode);
//                if (TextUtils.isEmpty(err)) {
//                    err = Utils.getResContext().getString(R.string.hwmconf_join_fail_tip);
//                }
//                DemoUtil.showToast("加入会议失败: " + err);
//                Log.e(TAG, "失败");
//            }
//
//            @Override
//            public void onCancel() {
//                Log.e(TAG, "取消");
//            }
//        });
//    }





}